<?php
if(isset($_REQUEST['aksi_get_pegawai'])){
	require_once('../class/class.pegawai.php');
	require_once('../class/class.cuti.php');

	session_start();
	$nip=$_SESSION['nip'];

	$pegawaiDao=new PegawaiDao();
	$cutiDao=new CutiDao();

	$pegawaiLogged=$pegawaiDao->getPegawai($nip);

	$unit=$pegawaiLogged['unit'];
	$tglSkrg=date('Y-m-d', strtotime($_REQUEST['tgl_cuti']));
	$msg=false;
	$cuti=array();
	$arr=array();
    $allPegawai=$pegawaiDao->getAllPegawai($unit);

    if($allPegawai!=false){
    	$msg=true;
    	for($i=0;$i<count($allPegawai);$i++){
	    	$cuti[$i]=$cutiDao->getAllCutiNow($allPegawai[$i]['nip'],$tglSkrg);
	    	if($cuti[$i]==false){
	    		$arr[$i]=array(
		    		'cutinya' => false,
		    		'pegawai' => $allPegawai[$i],
		    		);
	    	}else{
	    		$arr[$i]=array(
		    		'cutinya' => $cuti[$i],
		    		'pegawai' => $allPegawai[$i],
		    		);
	    	}
    	}
    }

    $data=array(
    	'msg' => $msg,
    	'cuti' => $arr,
    	);

    echo json_encode($data);
}

if(isset($_REQUEST['frm_aksi'])){
	if($_REQUEST['frm_aksi']=="detail"){
		require_once('../class/class.cuti.php');
		require_once '../class/class.pegawai.php';
		require_once '../class/class.refcuti.php';


		$id=$_REQUEST['id_aksi'];

		$cuti="";
		$pegawai="";
		$ref="";

		$cutiDao=new CutiDao();
		$cuti=$cutiDao->getCuti($id);
		$msg=false;
		

		if($cuti!=false){
			$msg=true;
			$pegawaiDao=new PegawaiDao();
			$pegawai=$pegawaiDao->getPegawai($cuti['nip']);
			$refDao=new RefCutiDao();
			$ref=$refDao->getCuti($cuti['idJnsCuti']);

		}

		$data=array(
				'msg'=> $msg,
				'cuti' => $cuti,
				'pegawai' => $pegawai,
				'ref' => $ref,
				);

		echo json_encode($data);
	}
	if($_REQUEST['frm_aksi']=="cetak"){
		include_once('../class/tbs_class.php');
		include_once('../class/plugins/tbs_plugin_opentbs.php');
		require_once('../class/class.pegawai.php');
		require_once('../class/class.cuti.php');
		require_once('../class/class.refcuti.php');
		require_once('../class/class.jabatan.php');
		require_once('../class/class.golongan.php');

		$TBS = new clsTinyButStrong;
		$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);

		$template = '../assets/rekap.xlsx';
		$TBS->LoadTemplate($template);

		session_start();
		$nip=$_SESSION['nip'];

		$pegawaiDao=new PegawaiDao();
		$cutiDao=new CutiDao();

		$pegawaiLogged=$pegawaiDao->getPegawai($nip);

		$unit=$pegawaiLogged['unit'];
		$tglSkrg=date('Y-m-d', strtotime($_POST['tgl_cuti']));
		$msg=false;
		$cuti=array();
		$arr=array();
		$refDao=new RefCutiDao();
		$jabatanDao=new JabatanDao();
	    $allPegawai=$pegawaiDao->getAllPegawai($unit);
	    $golonganDao= new GolonganDao();

	    if($allPegawai!=false){
	    	$msg=true;
	    	for($i=0;$i<count($allPegawai);$i++){
		    	$cuti[$i]=$cutiDao->getAllCutiNow($allPegawai[$i]['nip'],$tglSkrg);
		    	if($cuti[$i]!=false){
		    		$gol=$golonganDao->getGolongan($allPegawai[$i]['gol']);
		    		$jabat=explode("\n", $jabatanDao->getJabatan($allPegawai[$i]['nip']));
		    		$arr[$i]=array(
			    		'tglCuti' => $cuti[$i]['tglAwal']." s.d. ".$cuti[$i]['tglAkhir'],
			    		'nip' => $allPegawai[$i]['nip'],
			    		'nama' => $allPegawai[$i]['nama'],
			    		'ref' => $refDao->getCuti($cuti[$i]['idJnsCuti']),
			    		'jabatan' => $jabat[0],
			    		'golongan' => $gol['nmgol']." / ".$gol['pangkat'],
			    		'jmlHari' => $cuti[$i]['jmlHari'],
			    		);
		    	}
	    	}
	    }
	    // print_r($arr);
	    $TBS->MergeBlock('a', $arr);
		
		$output_file_name="rekap_cuti_tgl_".date('Y-m-d').".xlsx";
		$TBS->Show(OPENTBS_DOWNLOAD, $output_file_name);
	}
}
?>